
function SNFHandleAddonChatEvent(prefix, message, disttype, sender)
	if prefix == CHAT_PREFIX then
--		SNFDEBUG(2,"Chat Event Handled")
--		SNFDEBUG(3,prefix..":"..message.." From "..sender)
		SNFParseLootMsg(sender, message) -- can't find this funtion TODO
	end
	if (message == nil) then
		SNFDEBUG(2, "message is null in SNFHandleAddonChatEvent")
		return
	end
	if prefix == CHAT_PREFIX_SYNC then
		SNFParseSyncMessage(message, sender)
	end
	if prefix == CHAT_PREFIX_SMSYNC then
		SNFSmartSync:ParseMessage(message, sender)
	end
end

tblMarkers = {	["[bB][iI][sS]"] = "BIS",
		["best in slot"] = "BIS",
		["best"] = "BIS",
		["end game"] = "BIS",
		["EG"]  = "BIS",
		["[uU][gGpP]"]  = "UPG",
		["[dD][kK][pP]"]  = "DKP",
		["[oO][fF][fF]"] = "OFF",
		["side"] = "OFF"
	};


function SNFHandleGroupChatEvent(msg, sender)

	--msg = strlower(msg);
	local st = strfind(msg, "link")
	if not st then
		st = strfind(msg, "roll")
	end

	itemString, name = strmatch(msg, ".*|c%x+|H(.+)|h(.+)|h|r.*")

	if st and itemString then
--		SNFDEBUG(1, "Should be starting a new auction")
		itemid = strmatch(itemString, "item:(%d+):.*")
		_, itemLink = GetItemInfo(itemid);
		SNFAddNewAuction(itemLink, sender)
	elseif itemString then
		for text, cat in pairs(tblMarkers) do
			st = strfind(msg, text)
			if st then
				SNFDEBUG(1,"Found tag: "..cat.." from text "..text)
				SNFAddLootBid(cat,sender);
			end
		end
	end
end

local duplist = { }
local skipcount = 0

local function isDuplicate(item, name, chatter)
	if not duplist then duplist = { } end

	if not item or not name then
		SNFDEBUG(3,"Checking dup found bad key: item: "..(item or "nil").." name: "..(name or "nil"))
		return false
	end
	local key = item..name
	local nowtime = GetTime();

	if duplist[key] then
		if nowtime < duplist[key] + 2 then
			--we have an item being throttled, still within grace period
			skipcount = skipcount + 1;
			SNFDEBUG(4,"Skipping chat from "..chatter.." key is ".. key)
			return true
		end
	else
		duplist[key] = nowtime
		skipcount = 0
		SNFDEBUG(4, "Now throttling messages for key "..key.." first msg from "..chatter);
	end
	-- clean up very old items
	for key, thentime in pairs(duplist) do
		if nowtime > thentime + 10 then
			SNFDEBUG(3, "Cleaning up old throttling data")
			duplist[key] = nil
		end
	end
	return false
end


function SNFParseLootMsg(name, msg)

	item, count = strmatch(msg, SNFDKPPatterns["LOOT_ITEM_SELF_MULTIPLE"])
	if item then
		if not isDuplicate(item, name, name) then
			SNFCheckLootAgainstBids(name, item)
		end
		return true
	end

	who, item, count = strmatch(msg, SNFDKPPatterns["LOOT_ITEM_MULTIPLE"])
	if who and item then
		if not isDuplicate(item, who, name) then
			SNFCheckLootAgainstBids(who, item)
		end
		return true
	end

	item = strmatch(msg, SNFDKPPatterns["LOOT_ITEM_SELF"])
	if item then
		if not isDuplicate(item, name, name) then
			SNFCheckLootAgainstBids(name, item)
		end
		return true
	end

	who, item = strmatch(msg, SNFDKPPatterns["LOOT_ITEM"])
	if who and item then
		if not isDuplicate(item, who, name) then
			SNFCheckLootAgainstBids(who, item)
		end
		return true
	end

	return false
end

SNFDKPPatterns = { LOOT_ITEM = "", LOOT_ITEM_SELF = "", LOOT_ITEM_MULTIPLE = "", LOOT_ITEM_SELF_MULTIPLE = "" }

function SNFInitializeParser()

	for key, _ in pairs(SNFDKPPatterns) do
		local p = _G[key]
		SNFDKPPatterns[key] = gsub(gsub(p, "(%%s)", "(.+)"), "(%%d)", "(.+)")
	end
end

local function checkCanWaitList(name)

	local guildName, guildRankName, guildRankIndex = GetGuildInfo(name);

	if not guildName and not guildRankName then
		SNFDEBUG(1, "Unable to determine guild rank of "..name)
		return true
	end
	SNFDEBUG(1, "RankName is "..guildRankName.." and RankIndex is "..(guildRankIndex or "nil"))
	return true
end

function SNFHandleWhisperEvent(msg, sender)

	if (msg == nil) then
		SNFDEBUG(1, "SNFHandleWhisperEvent: Received nil message from "..(sender or "nil"))
		return
	end
	msg = strupper(msg)
	if strfind(msg, "WL[ ]*ADD") then
		SNFDEBUG(1, "Received waitlist add from "..sender)
		if (checkCanWaitList(sender)) then
			SNFAttend:WaitListAdd(sender)
		end
	end
	if strfind(msg, "WL[ ]*REMOVE") then
		SNFDEBUG(1, "Received waitlist remove from "..sender)
		SNFAttend:WaitListRemove(sender)
	end
	if strfind(msg, "?ATTEND.*") then
		SNFDEBUG(1, "Received attendance query from "..sender)
		SNFAttend:ReplyAttendance(sender)
	end
	if strfind(msg, "^?HELP ATTEND.*") then
		SNFDEBUG(1, "Received help query from "..sender)
		SNFMessage:QueueTell("Help Attendance key:", sender)
		SNFMessage:QueueTell("[YYYYMMDD] is the date that the raid data was recorded for.", sender)
		SNFMessage:QueueTell("S - In the raid at start.", sender)
		SNFMessage:QueueTell("E - In the raid at the end.", sender)
		SNFMessage:QueueTell("W - In the attendance wait list.", sender)
		SNFMessage:QueueTell("W can be for start or end or both.", sender)
		SNFMessage:QueueTell("X indicates that you received no credit for that start or end.", sender)
	end
	if strfind(msg, "?LOOT.*") then
		SNFDEBUG(1, "Received loot query from "..sender)
		SNFLoot:ReplyPastLoot(sender)
	end
	if strfind(msg, "?REPORT.*") then
		SNFDEBUG(1, "Received report query from "..sender)
		SNFAttend:ReplyRoleAttendanceReport(sender)
	end

end